package cn.doitedu.datagen.beans;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/***
 * @author hunter.d
 * @qq 657270652
 * @wx haitao-duan
 * @date 2021/12/15
 **/
public class EventResources {

    public static String[] beginEvents = {"revoke", "launch"};
    public static String[] endEvents = {"backoff", "exist"};

    public static Event getBeginEvent(){
        Event event = new Event();
        event.setEventId(beginEvents[RandomUtils.nextInt(0,beginEvents.length)]);
        event.setProperties(new HashMap<String, String>());
        return event;
    }

    public static Event getEndEvent(){
        Event event = new Event();
        event.setEventId(endEvents[RandomUtils.nextInt(0,endEvents.length)]);
        event.setProperties(new HashMap<String, String>());
        return event;
    }

    public static String[] eventIds = {
            "pageView",
            "productView",
            "promotionShow",
            "promotionClick",
            "categoryClick",
            "ColumnClick",
            "adShow",
            "adClick",
            "fetchCoupon",
            "signIn",
            "submitOrder",
            "addCart",
            "search",
            "collect",
            "thumbUp",
            "share",
            "login"
    };


    private static String[] columns = {
            "jobs", "courses", "teachers", "schools", "contacts", "students", "shares", "items", "search"
    };

    public static String getColumn() {
        return columns[RandomUtils.nextInt(0, columns.length)];
    }


    private static String[] courses = {
            "spark", "hadoop", "java", "python", "javaee", "javase", "flink",
            "kafka", "redis", "mongodb", "hudi", "iceberg", "mysql", "clickhouse",
            "azkaban", "sqoop", "datax", "atlas", "griffin", "livy", "shell", "linux",
            "kettle", "powerbi", "hive", "hbase", "kylin"
    };

    public static String getCourse() {
        return courses[RandomUtils.nextInt(0, courses.length)];
    }

    public static Page getPage() {
        Page page = new Page();
        String column = getColumn();
        if (column.equals("courses")) {
            page.pageId = "c" + StringUtils.leftPad(RandomUtils.nextInt(0, 100) + "", 3, "0");
            page.url = "/" + column + "/" + getCourse() + "/" + page.pageId + ".html";
        } else {
            page.pageId = column.substring(0, 3) + StringUtils.leftPad(RandomUtils.nextInt(0, 1000) + "", 4, "0");
            page.url = "/" + column + "/" + page.pageId + ".html";
        }
        return page;
    }


    public static Page getProductPage() {
        Page page = new Page();
            page.pageId = StringUtils.leftPad(RandomUtils.nextInt(0, 1000) + "", 5, "0");
            page.url = "/items/" + page.pageId + ".html";
        return page;
    }

    public static Page getSearchPage() {
        Page page = new Page();
        page.pageId = "s";
        page.url = "/search/s?";
        return page;
    }



    public static Event getEvent() {
        int eventId = RandomUtils.nextInt(1, 18);
        Event event = new Event();
        HashMap<String, String> properties = new HashMap<String, String>();
        event.setProperties(properties);
        Page page;
        switch (eventId) {
            case 1:
                event.setEventId("pageView");
                page = getPage();
                properties.put("pageId", page.pageId);
                properties.put("url", page.url);
                if (RandomUtils.nextInt(1, 1000) % 50 == 0) {
                    properties.put("utm_source", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_adId", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_loc", RandomUtils.nextInt(1, 20) + "");
                    properties.put("refUrl", RandomStringUtils.randomAlphabetic(2, 3).toLowerCase() + ".com/" + RandomStringUtils.randomAlphabetic(2, 6).toLowerCase() + ".html");
                }else {
                    properties.put("refUrl", getPage().url);
                }
                break;

            case 2:
                event.setEventId("productView");
                page = getProductPage();  // TODO
                properties.put("pageId", page.pageId);
                properties.put("url", page.url);
                if (RandomUtils.nextInt(1, 1000) % 50 == 0) {
                    properties.put("utm_source", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_adId", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_loc", RandomUtils.nextInt(1, 20) + "");
                    properties.put("refUrl", RandomStringUtils.randomAlphabetic(2, 3).toLowerCase() + ".com/" + RandomStringUtils.randomAlphabetic(2, 6).toLowerCase() + ".html");
                }else {
                    properties.put("refUrl", getPage().url);
                }
                properties.put("productId",StringUtils.leftPad(RandomUtils.nextInt(1,1000)+"",6,"0"));
                break;

            case 3:
                event.setEventId("promotionShow");
                page = getPage();
                properties.put("pageId", page.pageId);
                properties.put("url", page.url);
                if (RandomUtils.nextInt(1, 1000) % 50 == 0) {
                    properties.put("utm_source", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_adId", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_loc", RandomUtils.nextInt(1, 20) + "");
                    properties.put("refUrl", RandomStringUtils.randomAlphabetic(2, 3).toLowerCase() + ".com/" + RandomStringUtils.randomAlphabetic(2, 6).toLowerCase() + ".html");
                }else {
                    properties.put("refUrl", getPage().url);
                }
                properties.put("promotionId",StringUtils.leftPad(RandomUtils.nextInt(1,20)+"",3,"0"));
                break;

            case 4:
                event.setEventId("promotionClick");
                page = getPage();
                properties.put("pageId", page.pageId);
                properties.put("url", page.url);
                if (RandomUtils.nextInt(1, 1000) % 50 == 0) {
                    properties.put("utm_source", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_adId", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_loc", RandomUtils.nextInt(1, 20) + "");
                    properties.put("refUrl", RandomStringUtils.randomAlphabetic(2, 3).toLowerCase() + ".com/" + RandomStringUtils.randomAlphabetic(2, 6).toLowerCase() + ".html");
                }else {
                    properties.put("refUrl", getPage().url);
                }
                properties.put("promotionId",StringUtils.leftPad(RandomUtils.nextInt(1,20)+"",3,"0"));
                break;

            case 5:
                event.setEventId("categoryClick");
                page = getPage();
                properties.put("pageId", page.pageId);
                properties.put("url", page.url);
                if (RandomUtils.nextInt(1, 1000) % 200 == 0) {
                    properties.put("utm_source", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_adId", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_loc", RandomUtils.nextInt(1, 20) + "");
                    properties.put("refUrl", RandomStringUtils.randomAlphabetic(2, 3).toLowerCase() + ".com/" + RandomStringUtils.randomAlphabetic(2, 6).toLowerCase() + ".html");
                }else {
                    properties.put("refUrl", getPage().url);
                }
                properties.put("categoryId",StringUtils.leftPad(RandomUtils.nextInt(1,20)+"",3,"0"));
                break;

            case 6:
                event.setEventId("ColumnClick");
                page = getPage();
                properties.put("pageId", page.pageId);
                properties.put("url", page.url);
                if (RandomUtils.nextInt(1, 1000) % 200 == 0) {
                    properties.put("utm_source", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_adId", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_loc", RandomUtils.nextInt(1, 20) + "");
                    properties.put("refUrl", RandomStringUtils.randomAlphabetic(2, 3).toLowerCase() + ".com/" + RandomStringUtils.randomAlphabetic(2, 6).toLowerCase() + ".html");
                }else {
                    properties.put("refUrl", getPage().url);
                }
                properties.put("columnId",StringUtils.leftPad(RandomUtils.nextInt(1,20)+"",3,"0"));
                break;

            case 7:
                event.setEventId("adShow");
                page = getPage();
                properties.put("pageId", page.pageId);
                properties.put("url", page.url);
                if (RandomUtils.nextInt(1, 1000) % 200 == 0) {
                    properties.put("utm_source", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_adId", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_loc", RandomUtils.nextInt(1, 20) + "");
                    properties.put("refUrl", RandomStringUtils.randomAlphabetic(2, 3).toLowerCase() + ".com/" + RandomStringUtils.randomAlphabetic(2, 6).toLowerCase() + ".html");
                }else {
                    properties.put("refUrl", getPage().url);
                }
                properties.put("adId",StringUtils.leftPad(RandomUtils.nextInt(1,100)+"",3,"0"));
                break;
            case 8:
                event.setEventId("adClick");
                page = getPage();
                properties.put("pageId", page.pageId);
                properties.put("url", page.url);
                if (RandomUtils.nextInt(1, 1000) % 200 == 0) {
                    properties.put("utm_source", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_adId", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_loc", RandomUtils.nextInt(1, 20) + "");
                    properties.put("refUrl", RandomStringUtils.randomAlphabetic(2, 3).toLowerCase() + ".com/" + RandomStringUtils.randomAlphabetic(2, 6).toLowerCase() + ".html");
                }else {
                    properties.put("refUrl", getPage().url);
                }
                properties.put("adId",StringUtils.leftPad(RandomUtils.nextInt(1,100)+"",3,"0"));
                break;

            case 9:
                event.setEventId("fetchCoupon");
                page = getPage();
                properties.put("pageId", page.pageId);
                properties.put("url", page.url);
                if (RandomUtils.nextInt(1, 1000) % 200 == 0) {
                    properties.put("utm_source", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_adId", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_loc", RandomUtils.nextInt(1, 20) + "");
                    properties.put("refUrl", RandomStringUtils.randomAlphabetic(2, 3).toLowerCase() + ".com/" + RandomStringUtils.randomAlphabetic(2, 6).toLowerCase() + ".html");
                }else {
                    properties.put("refUrl", getPage().url);
                }
                properties.put("couponId",StringUtils.leftPad(RandomUtils.nextInt(1,20)+"",3,"0"));
                break;
            case 10:
                event.setEventId("signIn");
                page = getPage();
                properties.put("pageId", page.pageId);
                properties.put("url", page.url);
                if (RandomUtils.nextInt(1, 1000) % 200 == 0) {
                    properties.put("utm_source", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_adId", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_loc", RandomUtils.nextInt(1, 20) + "");
                    properties.put("refUrl", RandomStringUtils.randomAlphabetic(2, 3).toLowerCase() + ".com/" + RandomStringUtils.randomAlphabetic(2, 6).toLowerCase() + ".html");
                }else {
                    properties.put("refUrl", getPage().url);
                }
                break;
            case 11:
                event.setEventId("submitOrder");
                break;
            case 12:
                event.setEventId("addCart");
                page = getProductPage();
                properties.put("pageId", page.pageId);
                properties.put("url", page.url);
                if (RandomUtils.nextInt(1, 1000) % 200 == 0) {
                    properties.put("utm_source", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_adId", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_loc", RandomUtils.nextInt(1, 20) + "");
                    properties.put("refUrl", RandomStringUtils.randomAlphabetic(2, 3).toLowerCase() + ".com/" + RandomStringUtils.randomAlphabetic(2, 6).toLowerCase() + ".html");
                }else {
                    properties.put("refUrl", getPage().url);
                }
                properties.put("productId",StringUtils.leftPad(RandomUtils.nextInt(1,1000)+"",5,"0"));
                break;
            case 13:
                event.setEventId("search");
                page = getSearchPage();
                properties.put("pageId", page.pageId);
                properties.put("url", page.url);
                if (RandomUtils.nextInt(1, 1000) % 200 == 0) {
                    properties.put("utm_source", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_adId", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_loc", RandomUtils.nextInt(1, 20) + "");
                    properties.put("refUrl", RandomStringUtils.randomAlphabetic(2, 3).toLowerCase() + ".com/" + RandomStringUtils.randomAlphabetic(2, 6).toLowerCase() + ".html");
                }else{
                    properties.put("refUrl", getPage().url);
                }
                properties.put("productId",StringUtils.leftPad(RandomUtils.nextInt(1,1000)+"",5,"0"));
                properties.put("keyWord",StaticResources.getChineseKeyWord());
                break;
            case 14:
                event.setEventId("collect");
                page = getPage();
                properties.put("pageId", page.pageId);
                properties.put("url", page.url);
                if (RandomUtils.nextInt(1, 1000) % 200 == 0) {
                    properties.put("utm_source", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_adId", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_loc", RandomUtils.nextInt(1, 20) + "");
                    properties.put("refUrl", RandomStringUtils.randomAlphabetic(2, 3).toLowerCase() + ".com/" + RandomStringUtils.randomAlphabetic(2, 6).toLowerCase() + ".html");
                }else {
                    properties.put("refUrl", getPage().url);
                }
                if (RandomUtils.nextInt(1, 1000) % 40 == 0) {
                    properties.put("productId",StringUtils.leftPad(RandomUtils.nextInt(1,1000)+"",5,"0"));
                }else{
                    properties.put("itemId",StringUtils.leftPad(RandomUtils.nextInt(1,1000)+"",5,"0"));
                }
                break;
            case 15:
                event.setEventId("thumbUp");
                page = getPage();
                properties.put("pageId", page.pageId);
                properties.put("url", page.url);
                if (RandomUtils.nextInt(1, 1000) % 200 == 0) {
                    properties.put("utm_source", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_adId", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_loc", RandomUtils.nextInt(1, 20) + "");
                    properties.put("refUrl", RandomStringUtils.randomAlphabetic(2, 3).toLowerCase() + ".com/" + RandomStringUtils.randomAlphabetic(2, 6).toLowerCase() + ".html");
                }else {
                    properties.put("refUrl", getPage().url);
                }
                if (RandomUtils.nextInt(1, 1000) % 40 == 0) {
                    properties.put("productId",StringUtils.leftPad(RandomUtils.nextInt(1,1000)+"",5,"0"));
                }else{
                    properties.put("itemId",StringUtils.leftPad(RandomUtils.nextInt(1,1000)+"",5,"0"));
                }
                break;
            case 16:
                event.setEventId("share");
                page = getPage();
                properties.put("pageId", page.pageId);
                properties.put("url", page.url);
                if (RandomUtils.nextInt(1, 1000) % 200 == 0) {
                    properties.put("utm_source", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_adId", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_loc", RandomUtils.nextInt(1, 20) + "");
                    properties.put("refUrl", RandomStringUtils.randomAlphabetic(2, 3).toLowerCase() + ".com/" + RandomStringUtils.randomAlphabetic(2, 6).toLowerCase() + ".html");
                }else {
                    properties.put("refUrl", getPage().url);
                }
                if (RandomUtils.nextInt(1, 1000) % 20 == 0) {
                    properties.put("productId",StringUtils.leftPad(RandomUtils.nextInt(1,1000)+"",5,"0"));
                }else{
                    properties.put("itemId",StringUtils.leftPad(RandomUtils.nextInt(1,1000)+"",5,"0"));
                }
                break;
            case 17:
                event.setEventId("login");
                page = getPage();
                properties.put("pageId", page.pageId);
                properties.put("url", page.url);
                if (RandomUtils.nextInt(1, 1000) % 200 == 0) {
                    properties.put("utm_source", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_adId", RandomUtils.nextInt(1, 50) + "");
                    properties.put("utm_loc", RandomUtils.nextInt(1, 20) + "");
                    properties.put("refUrl", RandomStringUtils.randomAlphabetic(2, 3).toLowerCase() + ".com/" + RandomStringUtils.randomAlphabetic(2, 6).toLowerCase() + ".html");
                }else {
                    properties.put("refUrl", getPage().url);
                }
                break;
            default:
                break;
        }

        return event;
    }


    public static class Page {
        String pageId;
        String url;
    }

}
